package com.ithe.canteen.service.impl;

import com.ithe.canteen.mapper.CollectMapper;
import com.ithe.canteen.mapper.FoodMapper;
import com.ithe.canteen.pojo.Collect;
import com.ithe.canteen.pojo.Food;
import com.ithe.canteen.service.CollectService;
import com.ithe.canteen.util.CreatIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectMapper collectMapper;

    @Autowired
    private FoodMapper foodMapper;

    @Value("${file.url}")
    private String fileUrl;

    @Override
    public Integer addCollect(String foodID, String customerID) {
        if(!collectMapper.getCollectState(foodID, customerID)) {
            String collectID = CreatIDUtil.createID("CL");
            return collectMapper.addCollect(collectID, foodID, customerID);
        }
        return 2;
    }

    @Override
    public Integer deleteCollect(String foodID, String customerID) {
        if(collectMapper.getCollectState(foodID, customerID)) {
            return collectMapper.deleteCollect(foodID, customerID);
        }
        return 2;
    }

    @Override
    public boolean getCollectState(String foodID, String customerID) {
        return collectMapper.getCollectState(foodID, customerID);
    }

    @Override
    public List<Food> getCollectList(String customerID) {
        List<Collect> getResult = collectMapper.getCollectList(customerID);
        List<Food> foodList = new ArrayList<Food>();
        for (Collect collect : getResult) {
            Food food = foodMapper.getFoodById(collect.getCollectFood());
            String imagePath = fileUrl + food.getFoodImage();
            food.setFoodImage(imagePath);
            foodList.add(food);
        }
        return foodList;
    }
}
